/**
 * Created by Mark on 2018/4/19.
 * 订单列表的处理逻辑
 */

require("./order-list.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var navSide = require("page/common/nav-side/nav-side.js");
var _order = require("service/order-service.js");
var _mm = require("util/mm.js");
var templateOrderList = require("./order-list.string");
var Pagination = require("util/pagination/pagination.js");/* 导入分页模块 */

var page = {
    data : {
        listParam : {
            pageNum : _mm.getUrlParam("pageNum") || 1,
            pageSize : _mm.getUrlParam("pageSize") || 5
        }
    },
    init : function(){
        this.onLoad();
    },

    onLoad : function(){
        this.loadNavSide();//加载侧边导航
        this.loadOrderList();//加载订单列表
    },
    //1.页面初始化函数
    loadNavSide : function () {//1.1加载侧边导航
        navSide.init({
            name : "order-list",
        });
    },
    loadOrderList : function() {//1.2加载订单列表
        var listHTML = "";
        var listParam = this.data.listParam;
        var _this = this;
        $orderListContent = $(".order-list-con");
        _order.getOrderList(listParam,
            function (res) {//成功时的回调函数
                listHTML = _mm.renderHtml(templateOrderList,res.data);
                $orderListContent.html(listHTML);
                _this.loadPagination({//加载分页信息
                    hasPreviousPage : res.data.hasPreviousPage,//是否有上一页，boolean
                    prePage         : res.data.prePage,//上一页的编号
                    hasNextPage     : res.data.hasNextPage,//是否有上一页，boolean
                    nextPage        : res.data.nextPage,//下一页的编号
                    pageNum         : res.data.pageNum,//当前的页号
                    pages           : res.data.pages//总的页数
                });
            }
            ,
            function (err) {//失败时的回调函数
                if(err =="Not Found"){
                    _mm.errorTips("加载信息失败！稍后再试..");
                    return;
                }
                _mm.errorTips(err.msg);
            }
        );
    },

    //2.辅助函数
    loadPagination : function(pageInfo){//2.1加载分页信息
        var _this = this;
        this.pagination ? "" : (this.pagination = new Pagination());
        this.pagination.render($.extend({},pageInfo,{
            container : $(".pagination"),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadOrderList();
            }
        }));
    },
};
$(function(){
    page.init();
});

